package mumu.android.localstorage;

import mumu.android.R;
import mumu.android.lib.localstorage.NewsEn;
import mumu.android.lib.tool.MLog;
import mumu.android.lib.tool.Tools;
import android.R.integer;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import java.io.*;

import android.view.ViewGroup.LayoutParams;

public class filedf extends DialogFragment {

	static filedf instance;

	public static filedf getInstance() {
		if (instance == null) {
			instance = new filedf();
		}
		return instance;
	}

	Dialog dialog;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		if (dialog == null) {
			dialog = new Dialog(getActivity(), R.style.dialog);
			LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
			View view = layoutInflater.inflate(R.layout.dialog_listview, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			dialog.setContentView(R.layout.dialog_listview);
			dialog.setCanceledOnTouchOutside(true);
		}
		write(Tools.getSDPath() + File.separator + "mumu/dd.txt", "哈哈");
		String str = read(Tools.getSDPath() + File.separator + "mumu/dd.txt");
		Tools.toast(getActivity(), str);

		writeWriter(Tools.getSDPath() + File.separator + "mumu/dds.txt", "ss哈哈");
		String strs = readReader(Tools.getSDPath() + File.separator + "mumu/dds.txt");
		Tools.toast(getActivity(), strs);
		return dialog;
	}

	public String read(String filepath) {
		StringBuilder sb = new StringBuilder("");
		FileInputStream fInputStream = null;
		try {
			// 输入流
			fInputStream = new FileInputStream(filepath);
			byte[] bys = new byte[1024];
			int hasread = 0;
			while ((hasread = fInputStream.read(bys)) > 0) {
				sb.append(new String(bys, "utf-8"));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fInputStream != null)
				try {
					fInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return sb.toString();
	}
	
	public void write(String filepath, String str) {
		// 输出流
		try {
			File file = new File(filepath);
			// file.createNewFile();
			File dirFile = new File(file.getParent());
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			FileOutputStream fOutputStream = new FileOutputStream(filepath, true);
			byte[] bys = str.getBytes("utf-8");
			fOutputStream.write(bys);
			fOutputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String readReader(String filepath) {
		StringBuilder sb = new StringBuilder("");
		FileInputStream fInputStream = null;
		try {
			// 输入流
			fInputStream = new FileInputStream(filepath);
			int hasread = 0;
			InputStreamReader isr = new InputStreamReader(fInputStream, "utf8");
			while ((hasread = isr.read()) != -1) {
				sb.append((char) hasread);
			}
			isr.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fInputStream != null)
				try {
					fInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return sb.toString();
	}



	public void writeWriter(String filepath, String str) {
		try {
			File file = new File(filepath);
			File dirFile = new File(file.getParent());
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			FileOutputStream fOutputStream = new FileOutputStream(filepath, true);
			OutputStreamWriter osw = new OutputStreamWriter(fOutputStream, "utf-8");
			osw.write(str);
			osw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
